package com.hunan.api.catv.utils;

import org.springframework.util.ObjectUtils;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
import java.util.Map;
import java.util.TreeMap;

public class Hmacsha256Utils {
    public static String hmacsha256(String data, String secret) {
        byte[] bytes = null;
        try {
            SecretKey secretKey = new SecretKeySpec(secret.getBytes("UTF-8"), "HMACSHA256");
            Mac mac = Mac.getInstance("HmacSHA256");
            mac.init(secretKey);
            bytes = mac.doFinal(data.getBytes("UTF-8"));
        } catch (Exception e) {
            new Exception("签名失败", e);
        }
        return Base64.getEncoder().encodeToString(bytes);
    }

    public static String formatUrlMap(Map<String, String> paraMap, String secret) {
        Map<String, String> sortParams = new TreeMap<String, String>(paraMap);
        StringBuilder signStr = new StringBuilder();
        for (Map.Entry<String, String> entry : sortParams.entrySet()) {
            signStr.append(entry.getKey()).append(entry.getValue());
        }
        byte[] bytes = null;
        try {
            SecretKey secretKey = new SecretKeySpec(secret.getBytes("UTF-8"), "HMACSHA256");
            Mac mac = Mac.getInstance("HmacSHA256");
            mac.init(secretKey);
            bytes = mac.doFinal(signStr.toString().getBytes("UTF-8"));
        } catch (Exception e) {
            new Exception("签名失败", e);
        }
        return Base64.getEncoder().encodeToString(bytes);
    }

    public static String cpsSign(Map<String, String> paraMap, String secret) {
        TreeMap<String, String> sortedParams = new TreeMap<String, String>(paraMap);
        StringBuilder signStr = new StringBuilder();
        boolean first = true;
        for (Map.Entry<String, String> param : sortedParams.entrySet()) {
            String value = param.getValue();
            if (ObjectUtils.isEmpty(value)) {
                continue;
            }
            if ("null".equals(value) || ObjectUtils.isEmpty(value)) {
                continue;
            }
            if (first) {
                first = false;
            } else {
                signStr.append("&");
            }
            signStr.append(param.getKey()).append("=");
            signStr.append(value);
        }
        signStr.append("&secret_key=").append(secret);
        byte[] bytes = null;
        try {
            Mac mac = Mac.getInstance("HmacSHA256");
            SecretKey secretKey = new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256");
            mac.init(secretKey);
            bytes = mac.doFinal(signStr.toString().toLowerCase().toString().getBytes("UTF-8"));
        } catch (Exception e) {
            new Exception("签名失败", e);
        }
        return Base64.getEncoder().encodeToString(bytes);
    }


    public static String toupiaoSign(String data, String secret) {
        StringBuilder signStr = new StringBuilder();
        signStr.append("data=");
        signStr.append("[");
        signStr.append(data);
        signStr.append("]");
        String a = signStr.toString().toLowerCase();
        String c = a + "&secret_key=" + secret;
        byte[] bytes = null;
        try {
            Mac mac = Mac.getInstance("HmacSHA256");
            SecretKey secretKey = new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256");
            mac.init(secretKey);
            bytes = mac.doFinal(c.getBytes("UTF-8"));
        } catch (Exception e) {
            new Exception("签名失败", e);
        }
        return Base64.getEncoder().encodeToString(bytes);
    }


    public static void main(String[] args) {
//		Map<String, String> paraMap = new HashMap<String, String>();
//		Map<String, String> data = new HashMap<String, String>();
//		data.put("platform", "WNAPP");
//		data.put("smartCardCode", "310000010100002209");
//		data.put("userCode", "");
//		paraMap.put("data", data.toString());
//		paraMap.put("timestamp", System.currentTimeMillis() + "");
//		String secret = "3bda73617a5c4115b7b21c197965bc68";
//		String sign = cpsSign(paraMap, secret);
//		paraMap.put("sign", sign);
//		Map<String, String> paraMap = new HashMap<String, String>();
//		Map<String, String> data = new HashMap<String, String>();
//		data.put("platform", "WNAPP");
//		data.put("token", "");
//		data.put("payType", "00");
//		data.put("syncType", "BSS");
//		data.put("paynotifyurl", "https://testshop.leyoump.com/mp-cloud/api/pay/wxPayCallBack");
//		//data.put("redirectUrl", "");
//		data.put("syncNotifyUrl", "http://test/test/test");
//
//		Map<String, String> custInfo = new HashMap<String, String>();
//		custInfo.put("custname", "罗志钢");
//		custInfo.put("servid", "000200068968");
//		custInfo.put("custid", "");
//		custInfo.put("gdNo", "");
//		custInfo.put("mac", "");
//		custInfo.put("cardNo", "8731202368225182");
//		custInfo.put("area", "3100");
//		custInfo.put("city", "CS");
//		data.put("custInfo", custInfo.toString());
//
//		Map<String, String> order = new HashMap<String, String>();
//		order.put("seqId", "bad3a24e577c427ba009f3f7e3c3ef13");
//		order.put("orderTotalPrice", "100");
//		data.put("order", order.toString());
//
//		List<Map<String, String>> products = new ArrayList<Map<String,String>>();
//		Map<String, String> products0 = new HashMap<String, String>();
//		products0.put("cardno", "8731202368225182");
//		products0.put("count", "1");
//		products0.put("amount", "100");
//		products0.put("productName", "充值商品");
//		products.add(products0);
//		data.put("products", products.toString());
//
//		List<Map<String, String>> orderparamstr = new ArrayList<Map<String,String>>();
//		Map<String, String> orderparamstr0 = new HashMap<String, String>();
//		orderparamstr0.put("typeno", "8731202368225182");
//		orderparamstr0.put("prono", "hnyxtyzh80000999");
//		orderparamstr0.put("orderTotalamt", "100");
//		orderparamstr.add(orderparamstr0);
//		data.put("orderparamstr", orderparamstr.toString());
//
//
//		paraMap.put("data", data.toString());
//		paraMap.put("timestamp", System.currentTimeMillis() + "");
//		String secret = "3bda73617a5c4115b7b21c197965bc68";
//		String sign = cpsSign(paraMap, secret);
//		paraMap.put("sign", sign);

//		加票给用户
//		Map<String, Object> data = new TreeMap<String, Object>();
//		Map<String, Object> paraMap = new HashMap<String, Object>();
//		data.put("activityId", "10013");
//		data.put("fromSource", "3");
//		data.put("type", "1");
//		data.put("userId", "2");
//		paraMap.put("data",data);
//		String sign = toupiaoSign(GsonUtils.toJson(data), "XHWl2QzjP3vF5060XgtcVuTRCuMKjFh9");
//		paraMap.put("sign", sign);
//		RestTemplate restTemplate = new RestTemplate();
//		StringBuffer url = new StringBuffer("http://100.125.14.74:8086");
//		url.append("/MemberInterface/addTicketsToUser");
//		HttpHeaders headers = new HttpHeaders();
//		headers.add("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE);
//		HttpEntity<String> request = new HttpEntity<String>(GsonUtils.toJson(paraMap),headers);
//		ResponseEntity<String> response = restTemplate.postForEntity(url.toString(), request, String.class);
    }
}
